//登录和注册需要的User类
var User = require('../models/user');
//发表需要的Post类
var Post = require('../models/post');
//引入留言需要的Comment类
// var Comment = require('../models/comment');
//需要引入一个加密的模块
var crypto = require('crypto');
//引入multer插件
var multer = require('multer');
//插件的配置信息
var storage = multer.diskStorage({
  //这个是上传图片的地址.
  destination:function(req,file,cb){
    cb(null,'public/images')
  },
  //上传到服务器上图片的名字.
  filename:function(req,file,cb){
    cb(null,file.originalname)
  }
})
var upload = multer({storage:storage,size:10225});

/* GET home page. */
/*router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});*/

module.exports = function (app) {
  app.get('/',function (req,res) {
    Post.getAll(null,function(err,posts,total){
      if(err){
        posts = [];
      }
      var date = new Date();
      //保存当前时间的各种格式
      var time = {
        date:date,
        year:date.getFullYear(),
        month:(date.getMonth() + 1),
        day:date.getDate(),
        minute: date.getHours() + ':' +
        (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes())
      };
      res.render('index',{
        title:'新浪微博',
        user:req.session.user,
        posts:posts,
        time:time,
      })
    })
  })
  app.post('/',function (req,res) {
    var md5 = crypto.createHash('md5');
    var password = md5.update(req.body.password).digest('hex');
    User.get(req.body.name,function(err,user){
      if(!user){
        //说明用户名不存在
        req.flash('error','用户名不存在');
        return res.redirect('/');
      }
      //检查两次密码是否一样
      if(user.password != password){
        req.flash('error','密码错误');
        return res.redirect('/');
      }
      req.session.user = user;
      //console.log(req.session.user);
      res.redirect('/');
    })
  })
  app.get('/regist',function (req,res) {
    res.render('regist',{
      title:'微博注册',
      success:req.flash('success').toString(),
      error:req.flash('error').toString()
    })
  })
  app.post('/regist',function (req,res) {
    var name = req.body.name;
    var password = req.body.password;
    var password_re = req.body['password-repeat'];
    var email = req.body.email;
    var tel = req.body.tel;
    if(name == '' || password == '' || password_re == '' || email == ''){
      req.flash('error','所有信息必填');
      return res.redirect('/regist');
    }
    // 检查两次密码是否一致
    if(password_re != password){
      req.flash('error','两次输入密码不一致');
      return res.redirect('/regist');
    }
    var md5 = crypto.createHash('md5');
    var password = md5.update(req.body.password).digest('hex');
    
    var newUser = new User({
      name:name,
      password:password,
      email:email,
      tel:tel
    })
    User.get(newUser.name,function (err,user) {
      if(err){
        req.flash('error',err);
        return res.redirect('/regist');
      }
      if(user){
        req.flash('error','用户名已存在');
        return res.redirect('/regist');
      }
      newUser.save(function (err,user) {
        if(err){
          req.flash('error',err);
        }
        req.flash('success','注册成功');
        res.redirect('/regist');
      })
    })
  })
  app.get('/logout',function(req,res){
    //1.清除session
    //2.给用户提示
    //3.跳转到首页
    req.session.user = null;
    res.redirect('/');
  })
  app.post('/report',function (req,res) {
    var currentUser = req.session.user;
    var post = new Post(currentUser.name,req.body.textarea);
    post.save(function(err){
      if(err){
        req.flash('error',err);
        return res.redirect('/');
      }
      req.flash('success','发布成功');
      res.redirect('/');
    })
  })
  app.get('/comment',function (req,res) {
    res.render('comment',{
      title:'微博',
      user:req.session.user,
    })
  })
};
